package cool.webstudy.admin.service.impl;

import cn.hutool.http.HttpUtil;
import cool.webstudy.admin.constant.enums.rescode.CheckResCodeEnum;
import cool.webstudy.admin.exception.BusinessException;
import cool.webstudy.admin.model.dto.competition.team.QueryCompTeamDetailInfoDTO;
import cool.webstudy.admin.model.dto.competition.team.UpdateCompetitionTeamScoreDTO;
import cool.webstudy.admin.service.CompetitionTeamService;
import cool.webstudy.admin.service.ICheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author think
 * @date 2025/3/5
 * @description: 检测服务实现类
 */
@Service
public class CheckServiceImpl implements ICheckService {
    @Autowired
    private CompetitionTeamService compTeamService;
    @Override
    public void checkDown(List<QueryCompTeamDetailInfoDTO> dtoList) {
        List<QueryCompTeamDetailInfoDTO> downCompTeamUnCodeList = new ArrayList<>();
        ExecutorService executor = Executors.newFixedThreadPool(10);
        CountDownLatch latch = new CountDownLatch(dtoList.size());

        dtoList.forEach(dto -> {
            executor.submit(() -> {
                try {
                    String teamNetAddress = dto.getTeamNetAddress();
                    String address = "http://%s:80";
                    address = String.format(address, teamNetAddress);
                    String result = HttpUtil.get(address);
                    if (result == null) {
                        synchronized (downCompTeamUnCodeList) {
                            downCompTeamUnCodeList.add(dto);
                        }
                    }
                } catch (Exception e) {
                    throw new BusinessException(CheckResCodeEnum.RC_ERROR_CHECK_DOWN);
                } finally {
                    latch.countDown();
                }
            });
        });

        try {
            latch.await(); // 等待所有任务完成
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        executor.shutdown();
        //批量更新分数
        batchUpdateTeamScore(downCompTeamUnCodeList);
    }

    //批量更新分数
    private void batchUpdateTeamScore(List<QueryCompTeamDetailInfoDTO> dtoList) {
        if (Objects.isNull(dtoList) || dtoList.isEmpty()){
            return;
        }
        List<UpdateCompetitionTeamScoreDTO> updateCompetitionTeamScoreDTOList = new ArrayList<>();
        for (QueryCompTeamDetailInfoDTO dto : dtoList) {
            UpdateCompetitionTeamScoreDTO updateCompetitionTeamScoreDTO = new UpdateCompetitionTeamScoreDTO();
            updateCompetitionTeamScoreDTO.setCompUnCode(dto.getCompUnCode());
            updateCompetitionTeamScoreDTO.setTeamUnCode(dto.getTeamUnCode());
            Integer newScore = dto.getCurrentScore() - 100;
            //默认扣100分
            updateCompetitionTeamScoreDTO.setCurrentScore(newScore);
        }
        compTeamService.batchUpdateCompetitionTeamScore(updateCompetitionTeamScoreDTOList);
    }
}
